package client;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;

import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class SearchField extends JTextField {

	private ArrayList<FilterPanel> fp;
	private static String TITLE = "Search";

	public SearchField(FilterPanel... fp) {
		super(TITLE);
		this.setHorizontalAlignment(JTextField.CENTER);
		this.fp = new ArrayList<FilterPanel>();
		for(FilterPanel f : fp){
			this.fp.add(f);
		}
		getDocument().addDocumentListener(new DocumentListener() {
			public void changedUpdate(DocumentEvent e) {
				filter();
			}
			public void insertUpdate(DocumentEvent e) {
				filter();
			}
			public void removeUpdate(DocumentEvent e) {
				filter();
			}
		});
		addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent e) {
				if(getText().equals(TITLE)){
					setText("");
				}
			}
			public void focusLost(FocusEvent e) {
			}
			
		});
	}

	public void filter() {
		for (FilterPanel p : fp) {
			p.filter(getText());
		}
	}

}
